﻿using Excel = Microsoft.Office.Interop.Excel;

namespace BingSpatialDataServiceExcelAddin.BSDSTools.Models
{
    public class Settings
    {
        #region Constructor

        public Settings(Excel.Workbook workbook)
        {
            DataSourceName = WorkbookTools.GetPropertyValue(workbook, "DataSourceName") as string;
            EntityTypeName = WorkbookTools.GetPropertyValue(workbook, "EntityTypeName") as string;
            MasterKey = WorkbookTools.GetPropertyValue(workbook, "MasterKey") as string;
            QueryKey = WorkbookTools.GetPropertyValue(workbook, "QueryKey") as string;
            Culture = WorkbookTools.GetPropertyValue(workbook, "Culture") as string;

            string t = WorkbookTools.GetPropertyValue(workbook, "IsPublic") as string;
            bool b;
            if (t != null && bool.TryParse(t, out b))
            {
                IsPublic = b;
            }
        }

        #endregion

        #region Public Properties

        public string DataSourceName { get; set; }

        public string EntityTypeName { get; set; }

        public string MasterKey { get; set; }

        public string QueryKey { get; set; }

        public string Culture { get; set; }

        public bool IsPublic { get; set; }

        #endregion

        #region Public Methods

        public void Save(Excel.Workbook workbook)
        {
            WorkbookTools.SetPropertyValue(workbook, "DataSourceName", DataSourceName, Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString);
            WorkbookTools.SetPropertyValue(workbook, "EntityTypeName", EntityTypeName, Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString);
            WorkbookTools.SetPropertyValue(workbook, "MasterKey", MasterKey, Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString);
            WorkbookTools.SetPropertyValue(workbook, "QueryKey", QueryKey, Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString);
            WorkbookTools.SetPropertyValue(workbook, "Culture", Culture, Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString);
            WorkbookTools.SetPropertyValue(workbook, "IsPublic", IsPublic, Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeBoolean);
        }

        #endregion
    }
}
